package com.ssmTest.controller;

import com.google.gson.Gson;
import com.ssmTest.model.*;
import com.ssmTest.service.*;
import com.ssmTest.service.impl.MsgSent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
public class MobileController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private ISystemSetupService iSystemSetupService;
    @Autowired
    private IMachineUserInformationService iMachineUserInformationService;
    @Autowired
    private IUserInformationService iUserInformationService;
    @Autowired
    private ISecondHandService iSecondHandService;
    @Autowired
    private FileService fileService;
    @Autowired
    private DateTimeService dateTimeService;
    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private IInterviewsService iInterviewsService;

    @Autowired
    private IEmploymentAdService iEmploymentAdService;
    @Autowired
    private IJobAdService iJobAdService;
    @Autowired
    private IServiceService iServiceService;

    //移动端登录
    @RequestMapping(value = "/mobileUserLogin.do")
    @ResponseBody
    public Result mobileUserLogin(@RequestParam("username") String username, @RequestParam("password") String password, ModelMap modelMap) {
        int type = iUserService.userLogin(username, password, modelMap);
        //0管理员 1用户登录成功 2密码错误  3用户不存在
        Result result = new Result();
        result.setCode(type);
        if (type == 1) {
            result.setData(iUserService.selectUserPhone(username));
        }
        return result;
    }

    //移动端获取短信验证码
    @RequestMapping(value = "/getSMSCode.do")
    @ResponseBody
    public Result getSMSCode(@RequestParam("phone") String phone) {
        //0发送失败  1发送成功  2用户已存在
        Result result = new Result();
        if (iUserService.user_Check(phone))
            result.setCode(2);
        else {
            String code = MsgSent.sendmessage(phone);
            if (!code.equals("")) {
                result.setCode(1);
                result.setData(code);
            } else {
                result.setCode(0);
            }
        }
        return result;
    }

    //移动端注册
    @RequestMapping(value = "/mobileUserRegister.do")
    @ResponseBody
    public Result mobileUserRegister(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("invitation_code") String invitation_code, @RequestParam("customer_type") int customer_type) {
        // 1 注册成功 -1 用户重复或已存在 -2 验证码失效
        Result result = new Result();
        result.setCode(iUserService.userRegister(username, password, invitation_code, customer_type));
        return result;
    }


    //移动端获取更改密码短信验证码
    @RequestMapping(value = "/getSMSCodetoChangePw.do")
    @ResponseBody
    public Result getSMSCodetoChangePw(@RequestParam("username") String username) {
        //0发送失败  1发送成功  2用户不存在
        Result result = new Result();
        if (iUserService.user_Check(username)) {
            String code = MsgSent.sendmessage(username);
            if (!code.equals("")) {
                result.setCode(1);
                result.setData(code);
            } else {
                result.setCode(0);
            }
        } else {
            result.setCode(2);
        }
        return result;
    }

    //移动端修改密码
    @RequestMapping(value = "/mobileChangePw.do")
    @ResponseBody
    public Result mobileChangePw(@RequestParam("username") String username, @RequestParam("oldpassword") String oldpassword, @RequestParam("password") String password) {
        //-1用户不存在  0旧密码错误 1修改密码成功
        Result result = new Result();
        result.setCode(iUserService.changePw(username, oldpassword, password));
        return result;
    }

    //返回农机手发布的信息
    @RequestMapping(value = "/getMachineInformation.do")
    @ResponseBody
    public Result getMachineInformation(int pagenum, String city, String name) {
        //0不存在  1生成信息列表
        Result result = new Result();
        List<MachineUserInformation> list = null;
        if (name != null && !"".equals(name))
            list = iMachineUserInformationService.selectUserInformationByAndName(pagenum, city, name);
        else
            list = iMachineUserInformationService.selectUserInformationByNoName(pagenum, city);
        if (list.size() == 0)
            result.setCode(0);
        else {
            result.setCode(1);
            result.setData(list);
        }
        return result;
    }

    //返回用户发布的信息
    @RequestMapping(value = "/getUserInformation.do")
    @ResponseBody
    public Result getUserInformation(@RequestParam("pagenum") int pagenum, @RequestParam("city") String city, @RequestParam("name") String name) {
        //0不存在  1生成信息列表
        Result result = new Result();
        List<UserInformation> list = null;
        if (!name.equals(""))
            list = iUserInformationService.selectUserInformationByAndName(pagenum, city, name);
        else
            list = iUserInformationService.selectUserInformationByNoName(pagenum, city);
        if (list.size() == 0)
            result.setCode(0);
        else {
            result.setCode(1);
            result.setData(list);
        }
        return result;
    }


    //返回二手信息列表

    @RequestMapping(value = "/getSecondInformation.do")
    @ResponseBody
    public Result getSecondInformation(@RequestParam("pagenum") int pagenum, @RequestParam("city") String city, @RequestParam("name") String name) {
        //0不存在  1生成信息列表
        Result result = new Result();
        List<SecondHand> list = null;
        if (!name.equals(""))
            list = iSecondHandService.selectSecondByAndName(pagenum, city, name);
        else
            list = iSecondHandService.selectSecondByNoName(pagenum, city);
        if (list.size() == 0)
            result.setCode(0);
        else {
            result.setCode(1);
            result.setData(list);
        }
        return result;
    }

    //发布的订单
    @RequestMapping(value = "/orderDown.do")
    @ResponseBody
    public Result orderDown(@RequestParam("id") int id, @RequestParam("first_party") int first_party, @RequestParam("second_party") int second_party, @RequestParam("type") int type) {
        //id表示由哪个订单生成            type 1普通用户发布 2农机手发布     first_party 需求方，普通用户     second_party 供应方  农机手
        //-1该信息不存在 -2已被下单 -3订单状态异常    其它正数:生成的订单id
        Result result = new Result();
        if (type == 1)
            result.setCode(iUserInformationService.orderUserInformation(id, first_party, second_party));
        if (type == 2)
            result.setCode(iMachineUserInformationService.orderMachineInformation(id, first_party, second_party));
        return result;
    }


    //普通用户发表信息
    @RequestMapping(value = "/releaseUserInformation.do")
    @ResponseBody
    public Result releaseUserInformation(@RequestParam("json") String json) {
        //0失败 1成功
        Gson gson = new Gson();
        UserInformation userInformation = gson.fromJson(json, UserInformation.class);
        Result result = new Result();
        userInformation.setTime(dateTimeService.getTime());
        iUserInformationService.insertUserInformation(userInformation);
        result.setCode(1);
        return result;
    }

    //农机手用户发表信息
    @RequestMapping(value = "/releaseMachineInformation.do")
    @ResponseBody
    public Result releaseMachineInformation(@RequestParam("json") String json, MultipartFile multipartFile1, MultipartFile multipartFile2, MultipartFile multipartFile3) {
        //0失败 1成功
        Gson gson = new Gson();
        MachineUserInformation machineUserInformation = gson.fromJson(json, MachineUserInformation.class);
        String fileload = "";
        if (multipartFile1 != null) {
            fileload = fileload + " " + fileService.uploadFile(multipartFile1, "machineUserInformation" + machineUserInformation.getId());
            if (multipartFile2 != null) {
                fileload = fileload + " " + fileService.uploadFile(multipartFile2, "machineUserInformation" + machineUserInformation.getId());
                if (multipartFile3 != null) {
                    fileload = fileload + " " + fileService.uploadFile(multipartFile3, "machineUserInformation" + machineUserInformation.getId());
                }
            }
        }
        machineUserInformation.setTime(dateTimeService.getTime());
        machineUserInformation.setAgricultural_machinery_photos(fileload);
        Result result = new Result();
        iMachineUserInformationService.insertMachineUserInformation(machineUserInformation);
        result.setCode(1);
        return result;
    }

    //二手消息发布
    @RequestMapping(value = "/releaseSecondInformation.do")
    @ResponseBody
    public Result releaseSecondInformation(@RequestParam("json") String json, MultipartFile multipartFile1, MultipartFile multipartFile2, MultipartFile multipartFile3) {
        //0失败 1成功
        Gson gson = new Gson();
        SecondHand secondHand = gson.fromJson(json, SecondHand.class);
        String fileload = "";
        if (multipartFile1 != null) {
            fileload = fileload + " " + fileService.uploadFile(multipartFile1, "secondInformation" + secondHand.getId());
            if (multipartFile2 != null) {
                fileload = fileload + " " + fileService.uploadFile(multipartFile2, "secondInformation" + secondHand.getId());
                if (multipartFile3 != null) {
                    fileload = fileload + " " + fileService.uploadFile(multipartFile3, "secondInformation" + secondHand.getId());
                }
            }
        }
        secondHand.setTime(dateTimeService.getTime());
        secondHand.setPicture(fileload);
        Result result = new Result();
        iSecondHandService.insertSecondHand(secondHand);
        result.setCode(1);
        return result;
    }

    //用户信息完善
    @RequestMapping(value = "/informationPerfection.do")
    @ResponseBody
    public Result informationPerfection(@RequestParam("id") int id, MultipartFile group_photo, int gender, String work_year, String drving_license, String payment, String address, String planting_type) {
        //0用户不存在    1更新用户信息成功
        Result result = new Result();
        result.setCode(iUserService.informationPerfection(id, group_photo, gender, work_year, drving_license, payment, address, planting_type));
        return result;
    }

    //实名认证
    @RequestMapping(value = "/realNameAuthentication.do")
    @ResponseBody
    public Result realNameAuthentication(@RequestParam("id") int id, MultipartFile id_card1, MultipartFile id_card2, MultipartFile id_card3, String id_number) {
        //0用户不存在    1更新用户信息成功，待审核
        Result result = new Result();
        String card = "";
        card = card + fileService.uploadFile(id_card1, "idcard" + id);
        card = card + " " + fileService.uploadFile(id_card2, "idcard" + id);
//    card = card + " " + fileService.uploadFile(id_card3, "idcard" + id);
        result.setCode(iUserService.updateIDCard(id, card, id_number));
        return result;
    }

    //门户认证
    @RequestMapping(value = "/realGateway.do")
    @ResponseBody
    public Result realGateway(@RequestParam("id") int id, MultipartFile gateway1, MultipartFile gateway2, MultipartFile gateway3) {
        //0用户不存在    1更新用户信息成功，待审核
        Result result = new Result();
        String card = "";
        card = card + fileService.uploadFile(gateway1, "gateway1" + id);
        if (gateway2 != null)
            card = card + " " + fileService.uploadFile(gateway2, "gateway2" + id);
        if (gateway3 != null)
            card = card + " " + fileService.uploadFile(gateway2, "gateway3" + id);
        result.setCode(iUserService.updateGateway(id, card));
        return result;
    }

    //驾照/经纪人经营执照/公司营业执照
    @RequestMapping(value = "/drivingLicense.do")
    @ResponseBody
    public Result drivingLicense(@RequestParam("id") int id, MultipartFile license1, MultipartFile license2, MultipartFile license3) {
        //0用户不存在    1更新用户信息成功，待审核
        Result result = new Result();
        String card = "";
        card = card + fileService.uploadFile(license1, "license" + id);
//    card = card + " " + fileService.uploadFile(license1, "license" + id);
//    card = card + " " + fileService.uploadFile(license3, "license" + id);
        result.setCode(iUserService.updateLicense(id, card));
        return result;
    }


    //-------------------------------------查看订单信息-------------------------------

    //甲方first_party为普通用户、公司等  乙方second_party为农机手，员工等

    //农机手查看自己的作业以及找活订单
    //参数说明    id用户id    page 页码
    @RequestMapping(value = "/machineOrder.do")
    @ResponseBody
    public Result machineOrder(@RequestParam("id") int id, @RequestParam("page") int page) {
        //0用户不存在    1查询成功，数据存在
        Result result = new Result();
        if (iUserService.selectUser(id) == null)
            result.setCode(0);
        else {
            List<Order> list = iOrderService.selectOrders(id, page);
            result.setData(list);
            result.setCode(1);
        }
        return result;
    }


    //经纪人/用户查看自己的订单
    //参数说明    id用户id  ;  type订单类型  1招聘订单  2求职订单  3作业订单 4农机手找活订单(普通用户无法查询);   page 页码
    @RequestMapping(value = "/userOrder.do")
    @ResponseBody
    public Result userOrder(@RequestParam("id") int id, @RequestParam("type") int type, @RequestParam("page") int page) {
        //0用户不存在    1查询成功，数据存在
        Result result = new Result();
        if (iUserService.selectUser(id) == null) {
            result.setCode(0);
            return result;
        }
        if (type == 4) {
            List<Order> list = iOrderService.selectOrder(id, page);
            result.setData(list);
            result.setCode(1);
        } else if (type == 3) {
            List<Order> list = iOrderService.selectOrder(id, page);
            result.setData(list);
            result.setCode(1);
        } else if (type == 1) {
            List<Interviews> list = iInterviewsService.selectInterview(id, page);
            result.setData(list);
            result.setCode(1);
        } else if (type == 2) {
            List<Interviews> list = iInterviewsService.selectInterviews(id, page);
            result.setData(list);
            result.setCode(1);
        }

        return result;
    }


    //查询某账号邀请的用户
    @RequestMapping(value = "/invitCode.do")
    @ResponseBody
    public Result invitCode(@RequestParam("invitation_code") String invitation_code) {
        //1返回成功
        Result result = new Result();
        List<User> list = iUserService.selectUserByInvitCode(invitation_code);
        result.setCode(1);
        result.setData(list);
        return result;
    }


    //更新邀请码
    @RequestMapping(value = "/updateinvitCode.do")
    @ResponseBody
    public Result updateinvitCode(@RequestParam("id") int id, @RequestParam("invitation_code") String invitation_code) {
        //0用户不存在    1更新成功    2邀请码无效
        Result result = new Result();
        User user = iUserService.selectUser(id);
        if (user == null)
            result.setCode(0);
        else if (iUserService.selectInvitCode(invitation_code) == null)
            result.setCode(0);
        else {
            user.setInvitation_code(invitation_code);
            iUserService.updateUser(user);
            result.setCode(1);
        }
        return result;
    }

    //----------------------------------------------------------根据id查询信息---------------------------
    //查询农机手发布信息
    @RequestMapping(value = "/getMachineInformationById.do")
    @ResponseBody
    public Result getMachineInformationById(@RequestParam("id") int id) {
        //0信息不存在    1信息查询成功
        Result result = new Result();
        MachineUserInformation machineUserInformation = iMachineUserInformationService.selectMachineUserInformation(id);
        if (machineUserInformation == null)
            result.setCode(0);
        else {
            result.setCode(1);
            result.setData(machineUserInformation);
        }
        return result;
    }

    //查询普通用户发布信息
    @RequestMapping(value = "/getUserInformationById.do")
    @ResponseBody
    public Result getUserInformationById(@RequestParam("id") int id) {
        //0信息不存在    1信息查询成功
        Result result = new Result();
        UserInformation userInformation = iUserInformationService.selectUserInformation(id);
        if (userInformation == null)
            result.setCode(0);
        else {
            result.setCode(1);
            result.setData(userInformation);
        }
        return result;
    }

    //查询求职广告
    @RequestMapping(value = "/getJobADById.do")
    @ResponseBody
    public Result getJobADById(@RequestParam("id") int id) {
        //0信息不存在    1信息查询成功
        Result result = new Result();
        JobAd jobAd = iJobAdService.selectJobAd(id);
        if (jobAd == null)
            result.setCode(0);
        else {
            result.setCode(1);
            result.setData(jobAd);
        }
        return result;
    }

    //查询招聘广告
    @RequestMapping(value = "/getEmploymentADById.do")
    @ResponseBody
    public Result getEmploymentADById(@RequestParam("id") int id) {
        //0信息不存在    1信息查询成功
        Result result = new Result();
        EmploymentAd employmentAd = iEmploymentAdService.selectEmploymentAd(id);
        if (employmentAd == null)
            result.setCode(0);
        else {
            result.setCode(1);
            result.setData(employmentAd);
        }
        return result;
    }

    //-----------------------------------------------查询自己的发布记录----------------


    @RequestMapping(value = "/getHistory.do")
    @ResponseBody
    public Result getHistory(@RequestParam("id") int id, @RequestParam("type") String type) {
        //传参 id persional_id     type  1农机手发布信息MachineInformation 2普通用户发布信息UserInformation 3求职广告JobAd  4 招聘广告 employmentAd  5 农信息Orders  6招聘信息 Interviews  7二手信息SecondHand 8服务Services
        // 返回值   0用户不存在 1成功
        Result result = new Result();
        if (iUserService.selectUser(id) == null) {
            result.setCode(0);
            return result;
        }
        if ("1".equals(type)) {
            List list = iMachineUserInformationService.selectByPersionalId(id);
            result.setData(list);
        } else if ("2".equals(type)) {
            List list = iUserInformationService.selectByPersionalId(id);
            result.setData(list);
        } else if ("3".equals(type)) {
            List list = iJobAdService.selectByPersionalId(id);
            result.setData(list);
        } else if ("4".equals(type)) {
            List list = iEmploymentAdService.selectByPersionalId(id);
            result.setData(list);
        } else if ("5".equals(type)) {
            List list = iOrderService.selectByPersionalId(id);
            result.setData(list);
        } else if ("6".equals(type)) {
            List list = iInterviewsService.selectByPersionalId(id);
            result.setData(list);
        } else if ("7".equals(type)) {
            List list = iSecondHandService.selectByPersionalId(id);
            result.setData(list);
        } else if ("8".equals(type)) {
            List list = iServiceService.selectByPersionalId(id);
            result.setData(list);
        }
        result.setCode(1);
        return result;
    }

    //更改订单状态信息
    @RequestMapping(value = "/exchangeOrderToexamine.do")
    @ResponseBody
    public Result exchangeOrderToexamine(@RequestParam("id") int id, @RequestParam("toexamine") int toexamine) {
        //id 订单id    toexamine   0订单初始化 1已付款 2已完工 3资金分配完毕完结(同数据库)
        //0订单信息不存在    1信息修改成功
        Result result = new Result();
        Order order = iOrderService.selectOrderByID(id);
        if (order == null) {
            result.setCode(0);
        } else {
            iOrderService.updateOrderToexamine(id, toexamine);
            result.setCode(1);
        }
        return result;
    }


}

